import { likePost } from '@/api/home'
import type { likePostReqType } from '@/types/home'
import { showSuccessToast } from 'vant'
import { ref } from 'vue'

export const likeFlagEvent = (type: likePostReqType = 'doc') => {
  const loading = ref(false)
  //   ts向下兼容特性:形参的类型可以小一些，实参可以大一些，只要实参包容形参类型就可以了
  // function fn(a:{id:string}){}   fn({id:1,name:123,age:18})
  const likeEvent = async (itemData: { likeFlag: number; id: string }) => {
    loading.value = true
    await likePost({
      type: type,
      id: itemData.id
    })
    // 0==>1  1==>0
    itemData.likeFlag = 1 - itemData.likeFlag
    loading.value = false
    showSuccessToast(itemData.likeFlag === 1 ? '关注成功' : '取消关注')
  }
  return { loading, likeEvent }
}
